<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MOauthServerDecide
extends OAuthServer
{
    
    public function accessToken()
    {
        $consumer_key = $this->getParam('oauth_consumer_key');
        $token        = $this->getParam('oauth_token');

        $server_token = new MServerToken();
        $server_token_status = $server_token->queryOauthByToken($token);
        if ($server_token_status == false){
            parent::accessToken();
            return;
        }

                $ost__user_id = $server_token->ost_usa_id_ref;
        $server_token_array  = $server_token->queryOauthByIdAndTokenType($ost__user_id, "access");
        if (empty($server_token_array)){
            parent::accessToken();
            return;
        }
        
                $server_register_handle = new MServerRegistry();
        $server_register = $server_register_handle->queryRegistryByID($server_token_array[0]["ost_osr_id_ref"]);
        if (empty($server_register)){
            throw new MAuthorizationException("oauth_consumer_key not exist", 401);
        }
        if ($server_register->osr_consumer_key != $consumer_key){
            parent::accessToken();
            return;
        }

        $data      = $this->verify('request');
        $device_id = $data['user_id'];

        if (empty($device_id)){
            throw new MAuthorizationException("oauth_valid_error", 401);
        }

                $server_token->deleteOauthByTokenAndTokenType($device_id, "request");

        $server_token_access = MServerToken::initServerToken($server_token_array[0]);

        $result = 'oauth_token='.$this->urlencode($server_token_access->ost_token).'&oauth_token_secret='.$this->urlencode($server_token_access->ost_token_secret);

        header('HTTP/1.1 200 OK');
        header('Content-Length: '.strlen($result));
        header('Content-Type: application/x-www-form-urlencoded');

        echo $result;
    }


    
    public function authorizeFinish ( $authorized, $user_id )
    {
        $verifier = parent::authorizeFinish($authorized, $user_id);

        if (empty($oauth_callback))
        {
            echo $verifier;
        }
    }
}
?>